package com.hzystudynote;

import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.TreeMap;

/** @author ASUS */
public class KeyClass {
  public static void main(String[] args) {
    TreeMap<Student, String> tests = new TreeMap<>();
    tests.put(new Student("123456", "qaq", 15, "male", 95), "优秀");
    tests.put(new Student("123456", "www", 15, "male", 90), "优秀");
    tests.put(new Student("123456", "eee", 15, "male", 92), "优秀");
    tests.put(new Student("123456", "qaq", 15, "male", 93), "优秀");
    tests.put(new Student("123457", "qcq", 15, "male", 95), "优秀");
    tests.put(new Student("123458", "qbq", 15, "male", 95), "优秀");
    for (Map.Entry<Student, String> entry : tests.entrySet()) {
      System.out.println(entry.getKey());
      System.out.println(entry.getValue());
    }
    String tmp = tests.get(new Student("123458", "qbq", 15, "male", 95));
    System.out.println(tmp);
  }
}

class Student implements Comparable<Student> {
  private final int age;
  private final String sex;
  private final int scores;
  public String id;
  public String name;

  public Student(String id, String name, int age, String sex, int scores) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.scores = scores;
  }

  @Override
  public String toString() {
    return "Student [name="
        + name
        + ", age="
        + age
        + ", sex="
        + sex
        + ", scores="
        + scores
        + ", id="
        + id
        + "]";
  }

  @Override
  public int hashCode() {
    int result = name.hashCode();
    result = 100 * result + sex.hashCode();
    result = 100 * result + age;
    result = 100 * result + id.hashCode();
    result = 100 * result + scores;
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Student)) {
      // instanceof 已经处理了obj = null的情况
      return false;
    }
    Student stuObj = (Student) obj;
    // 地址相等
    if (this == stuObj) {
      return true;
    }
    // 如果两个对象姓名、年龄、性别相等，我们认为两个对象相等
    return this.id.equals(stuObj.id);
  }

  @Override
  public int compareTo(@NotNull Student o) {
    // return this.scores - o.scores;
    return this.id.compareTo(o.id);
  }
}
